/*
 * Copyright (c) 2018-present, 美团点评
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#import <objc/runtime.h>
#import <objc/message.h>
#import "FlutterLoganPlugin.h"
#import "Logan.h"

@implementation FlutterLoganPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_logan"
            binaryMessenger:[registrar messenger]];
  FlutterLoganPlugin* instance = [[FlutterLoganPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

+ (NSString *)loganLogDirectory {
    static NSString *dir = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"LoganLoggerv3"];
    });
    return dir;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSLog(@"call method %@ args=%@",call.method,call.arguments);
    SEL sel = NSSelectorFromString([call.method stringByAppendingString:@":result:"]);
    if(sel && [self respondsToSelector:sel]){
        ((void(*)(id,SEL,id,id))objc_msgSend)(self,sel,call.arguments,result);
    }else{
        result(FlutterMethodNotImplemented);
    }
}

- (void)init:(NSDictionary *)args result:(FlutterResult)result{
    if(![args isKindOfClass:[NSDictionary class]]){
        result(@(NO));
        return;
    }
    NSString *key = args[@"aesKey"];
    NSString *iv = args[@"aesIv"];
    NSNumber *maxFileLen = args[@"maxFileLen"];
    if(key.length >0 && iv.length >0){
        loganInit([NSData dataWithBytes:key.UTF8String length:key.length],[NSData dataWithBytes:iv.UTF8String length:iv.length] , maxFileLen.integerValue);
        result(@(YES));
    }else{
        result(@(NO));
    }
}

- (void)log:(NSDictionary *)args result:(FlutterResult)result{
    if(![args isKindOfClass:[NSDictionary class]]){
        result(nil);
        return;
    }
    NSNumber *type = args[@"type"];
    NSString *log = args[@"log"];
    logan(type.integerValue, log);
    NSLog(@"正在写入");
    result(nil);
}

- (void)flush:(NSDictionary *)args result:(FlutterResult)result{
    loganFlush();
    result(nil);
}

- (void)getLoganDir:(NSDictionary *)args result:(FlutterResult)result{
    result([FlutterLoganPlugin loganLogDirectory]);
}

- (void)upload:(NSDictionary *)args result:(FlutterResult)result{
    if(![args isKindOfClass:[NSDictionary class]]){
        result(@NO);
        return;
    }
    NSString *urlStr = args[@"serverUrl"];
    NSString *date = args[@"date"];
    NSString *appid = args[@"appId"];
    NSString *unionId = args[@"unionId"];
    NSString *deviceId = args[@"deviceId"];
    NSString *appVersion = args[@"appVersion"];
    BOOL deleteFile = [args[@"delete"] boolValue];
    // 检查必要参数
    if (![date length] || ![urlStr length]) {
        result(@"args is empty");
        return;
    }
    loganUpload(urlStr,date,appid,unionId,deviceId,^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error){
        if(error){
            result(@NO);
        }else{
            if(deleteFile) {
                NSString *filePath = [[FlutterLoganPlugin loganLogDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",date]];
                 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
                     NSLog(@"Logan: Delete logan file: %@", filePath);
                     [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
                 }
            }
            result(@YES);
        }
    });
}

- (void)cleanAllLogs:(NSArray *)param result:(FlutterResult)result{
    loganClearAllLogs();
    result(nil);
}



@end
